package club.banyuan.hrms.dao.impl;

import club.banyuan.hrms.dao.EmployeeDao;
import club.banyuan.hrms.domain.Employee;
import club.banyuan.hrms.domain.Position;

import java.util.ArrayList;
import java.util.List;

public class EmployeeDaoImpl implements EmployeeDao {
    private static List<Employee> employees;

    static {
        employees = new ArrayList<>();

        Employee employee = new Employee();
        employee.setId(1);
        employee.setName("zhangsan");
        employee.setSex("male");
        employee.setPhone("15358529771");
        employee.setEmail("1132344476@qq.com");
        employee.setAddress("地址1");
        employee.setEducation("Bachelor");
        employee.setBirthday("1988-12-25");
        employee.setDepartmentId(1);
        employee.setDepartmentName("研发部");
        employee.setPositionId(1);
        employee.setPositionName("项目经理");
        employees.add(employee);

        employee = new Employee();
        employee.setId(2);
        employee.setName("lisi");
        employee.setSex("female");
        employee.setPhone("15358529772");
        employee.setEmail("1132344477@qq.com");
        employee.setAddress("地址2");
        employee.setEducation("Master");
        employee.setBirthday("1998-06-25");
        employee.setDepartmentId(2);
        employee.setDepartmentName("产品部");
        employee.setPositionId(2);
        employee.setPositionName("产品经理");
        employees.add(employee);
    }

    @Override
    public List<Employee> getEmployees() {
        return employees;
    }

    @Override
    public void updateEmployee(Employee employee) {
        Integer id = employee.getId();
        if (id != null) {
            Employee existEmployee = getEmployeeById(id);

            existEmployee.setName(employee.getName());
            existEmployee.setSex(employee.getSex());
            existEmployee.setPhone(employee.getPhone());
            existEmployee.setEmail(employee.getEmail());
            existEmployee.setAddress(employee.getAddress());
            existEmployee.setEducation(employee.getEducation());
            existEmployee.setBirthday(employee.getBirthday());
            existEmployee.setDepartmentId(employee.getDepartmentId());
            existEmployee.setDepartmentName(employee.getDepartmentName());
            existEmployee.setPositionId(employee.getPositionId());
            existEmployee.setPositionName(employee.getPositionName());
        }
    }

    @Override
    public void addEmployee(Employee employee) {
//        employee.setId(employees.size() + 1);
        employees.add(employee);
    }

    @Override
    public Employee getEmployeeById(Integer id) {
        for (Employee employee : employees) {
            if (employee.getId().equals(id)) {
                return employee;
            }
        }
        return null;
    }


}
